package com.example.shop_car_2208.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TableLayout
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.R
import com.example.shop_car_2208.adapter.MyAdapter
import com.example.shop_car_2208.databinding.FragmentMyBinding
import com.example.shop_car_2208.db.MyDBUtils
import com.example.shop_car_2208.mvvm.BaseFragment

import com.example.shop_car_2208.mvvm.BaseViewFragment
import com.example.shop_car_2208.utils.GlideUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

@Route(path = "/app/my")
class MyFragment : BaseFragment<FragmentMyBinding, MyViewModel>() {
    val list = mutableListOf<Fragment>()
    val title = mutableListOf<String>()
    lateinit var myAdapter: MyAdapter
    override fun initData() {
        viewModel.success.observe(this){
            if (it.code == 200){
                ToastUtils.showLong(it.message)

            }
        }
    }

    override fun initView() {
        lifecycleScope.launch {
            binding.user = MyDBUtils.getDB().getUserDao().getUserById(SPUtils.getInstance().getInt("id"))
        }

        list.add(PayingFragment())
        list.add(PayedFragment())
        title.add("待支付")
        title.add("已支付")
        myAdapter = MyAdapter(requireActivity(),list)
        binding.vp.adapter = myAdapter
        TabLayoutMediator(binding.tab,binding.vp,object :TabLayoutMediator.TabConfigurationStrategy{
            override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                tab.text = title[position]
            }
        }).attach()
        binding.userIcon.setOnClickListener {
            PictureSelector.create(this,103).selectPicture()
        }

    }

    override fun getViewModel(): Class<MyViewModel> {
        return MyViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_my
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 103 && resultCode == Activity.RESULT_OK){
            val pic = data?.extras?.get(PictureSelector.PICTURE_RESULT)as PictureBean
            context?.let { GlideUtils.loafCircle(it,binding.userIcon,pic.path) }

            //调用更新后台
            val body = RequestBody.create("multipart/form-data".toMediaType(), File(pic.path))
            val part = MultipartBody.Part.createFormData("file","abc.jpg",body)
            viewModel.updateIcon(part )
        }
    }
}